package com.mud.mudcommune.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mud.mudcommune.R;
import com.mud.mudcommune.entry.CaseEntry;
import com.mud.mudcommune.entry.CaseGalleryEntry;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/15 0015.
 */

public class HotRecommendAdapter extends MyBaseAdapter {
    private static final String TAG = HotRecommendAdapter.class.getSimpleName();
    private Context context;
    private List<CaseEntry> caseList;

    public HotRecommendAdapter(Context context, @NonNull List<CaseEntry> caseList) {
        super(caseList);
        this.context = context;
        this.caseList = caseList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        CaseEntry caseEntry = caseList.get(position);
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_image, null);
            getViewHolder(convertView);
        }
        viewHolder = getViewHolder(convertView);
        List<CaseGalleryEntry> caseGalleryList = caseEntry.getCaseGalleryList();
        if (caseGalleryList != null && caseGalleryList.size() > 0) {//glide直接加载图片
            Glide.with(context).load(caseGalleryList.get(0).getImgUrl()).into(viewHolder.image);
        }
        viewHolder.title.setText(caseEntry.getCaseName()); // title
        viewHolder.details.setText(caseEntry.getCaseDesc());
        if (caseEntry.getAddTime() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(Long.valueOf(caseEntry.getAddTime()));
            // 将时间戳换成日期格式显示
            viewHolder.time.setText(simpleDateFormat.format(date));
        }
        return convertView;
    }

    public ViewHolder getViewHolder(View convertView) {
        ViewHolder viewHolder = null;
        viewHolder = (ViewHolder) convertView.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder(convertView);
        }
        return viewHolder;
    }

    static class ViewHolder {
        ImageView image;
        TextView title;
        TextView details;
//      TextView starNum;
//      TextView eyeNum;
        TextView time;

        public ViewHolder(View convertView) {
            image = ButterKnife.findById(convertView, R.id.image);
            title = ButterKnife.findById(convertView, R.id.text_title);
            details = ButterKnife.findById(convertView, R.id.text_details);
//            starNum = ButterKnife.findById(convertView, R.id.star_num);
//            eyeNum = ButterKnife.findById(convertView, R.id.eye_num);
            time = ButterKnife.findById(convertView, R.id.time);
            convertView.setTag(this);
        }
    }
}